﻿using AutoMapper;
using Common.Infrastructrue.Interface;
using Common.Service;
using MediatR;

namespace RabitPart.ReadApi.Application.RBACrelationship.RELUserShow
{
    public class ShowRel_user_roleCommanHandel : IRequestHandler<ShowRel_user_roleComman, ApiResult<IList<ShowRel_user_roleCommanResponse>>>
    {
        private readonly IMapper mapper;
        private readonly IRel_user_roleResponstity useroleRE;
        private readonly IUserResponstity userRE;  
        private readonly IRoleResponstity RoleRE;  
        private readonly ILogger<ShowRel_user_roleCommanHandel> logger;
        public ShowRel_user_roleCommanHandel(IMapper mapper, IRel_user_roleResponstity useroleRE, IUserResponstity userRE, IRoleResponstity RoleRE,ILogger<ShowRel_user_roleCommanHandel> logger)
        {
            this.mapper = mapper;
            this.useroleRE = useroleRE;
            this.logger = logger;
            this.userRE = userRE;
            this.RoleRE = RoleRE;
        }

        public async Task<ApiResult<IList<ShowRel_user_roleCommanResponse>>> Handle(ShowRel_user_roleComman request, CancellationToken cancellationToken)
        {
            var userlist = (await userRE.GetAllAsync()).ToList();
            var rolelist = (await RoleRE.GetAllAsync()).ToList();
            var userolelist = (await useroleRE.GetAllAsync()).ToList();

            var list = from a in userlist
                       join b in userolelist on a.uId equals b.uId
                       join c in rolelist on b.rId equals c.rId
                       select new ShowRel_user_roleCommanResponse
                       {
                           userName = a.userName,
                           userpass = a.userpass,
                           userNickname = a.userNickname, 
                           roleName = c.roleName,
                           urId = b.urId,
                           rId = b.rId,
                           uId = b.uId
                       };
            var result = mapper.Map<IList<ShowRel_user_roleCommanResponse>>(list);

            return await Task.FromResult(ApiResult<IList<ShowRel_user_roleCommanResponse>>.Success(result, StatusCodeEnum.Ok));
        }
    }
}
